﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Relocation.Base;
using Relocation.Data;
using Relocation.Com;


namespace Relocation.UI
{
	public partial class UserList : BaseUserList
	{
		public UserList()
			: base()
		{
			InitializeComponent();
			Initialize();
		}

		public UserList(Session session)
			: base(session)
		{
			InitializeComponent();
			Initialize();
			this.Refresh();
		}

		/// <summary>
		/// Refreshes this instance.
		/// </summary>
		public override void Refresh()
		{
			this.Reload();
		}

		private void Initialize()
		{
			this.S_UserName.Tag = new ControlTag(null, null, new SearchInfo(("name")));
			this.SortField = "name";
			this.SortDirection = ListSortDirection.Ascending;
			this.ObjectQuery = this.Session.DataModel.Users;
		}

		/// <summary>
		/// 添加用户
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		/// <remarks></remarks>
		private void AddNewItem_Click(object sender, EventArgs e)
		{
			using (UserWindow userWindow = new UserWindow(this.Session))
			{
				userWindow.Text = "创建用户";
				userWindow.ShowDialog();
			}
			this.Refresh();
		}

		private void userGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
		{
			string dataName = this.dataGridView1.Columns[e.ColumnIndex].DataPropertyName;
			if (string.IsNullOrEmpty(dataName))
				return;
			if (dataName.Equals("role"))
			{
				String e_vlaue = e.Value as String;
				if (string.IsNullOrEmpty(e_vlaue))
					return;
				e.Value = e_vlaue.Equals(Session.KEY_ROLE_ADMIN) ? "管理员" : e_vlaue.Equals(Session.KEY_ROLE_OPERATOR) ? "操作员" : "普通用户";

			}
		}

		

		/// <summary>
		/// 删除按钮事件
		/// </summary>		
		private void ButtonDel_Click(object sender, EventArgs e)
		{
			try
			{
				User user = this.GetSelectEntity();
				if (user == null)
					return;
				//不能删除当前用户
				if (user.user_id.Equals(this.Session.User.user_id))
				{
					MessageBoxShowInfo("不能删除当前用户！");
					return;
				}
				this.DeleteEntity(user);
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MessageBoxError("删除失败！");
			}
		}

		private void ButtonEdit_Click(object sender, EventArgs e)
		{
			try
			{
				User user = this.GetSelectEntity();
				if (user == null)
					return;
				using (UserWindow userWindow = new UserWindow(this.Session, user))
				{
					if (userWindow.ShowDialog(this).Equals(DialogResult.OK))
					{
						this.Reload();
					}
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MessageBoxError("修改失败！");
			}
		}

		private void ButtonAdd_Click(object sender, EventArgs e)
		{
			try
			{
				using (UserWindow userWindow = new UserWindow(this.Session))
				{
					if (userWindow.ShowDialog().Equals(DialogResult.OK))
					{
						this.Reload();
					}
				}
			} catch (Exception ex)
			{
				Log.Error(ex.GetInnerExceptionMessage());
				MessageBoxError("添加失败！");
			}
		}
	}
}
